{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Neural Network with One hidden layer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "# Importing the libraries\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Importing the dataset\n",
    "dataset = pd.read_csv('Churn_Modelling.csv')\n",
    "X = dataset.iloc[:, 3:13].values\n",
    "y = dataset.iloc[:, 13].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>RowNumber</th>\n",
       "      <th>CustomerId</th>\n",
       "      <th>Surname</th>\n",
       "      <th>CreditScore</th>\n",
       "      <th>Geography</th>\n",
       "      <th>Gender</th>\n",
       "      <th>Age</th>\n",
       "      <th>Tenure</th>\n",
       "      <th>Balance</th>\n",
       "      <th>NumOfProducts</th>\n",
       "      <th>HasCrCard</th>\n",
       "      <th>IsActiveMember</th>\n",
       "      <th>EstimatedSalary</th>\n",
       "      <th>Exited</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>15634602</td>\n",
       "      <td>Hargrave</td>\n",
       "      <td>619</td>\n",
       "      <td>France</td>\n",
       "      <td>Female</td>\n",
       "      <td>42</td>\n",
       "      <td>2</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>101348.88</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>15647311</td>\n",
       "      <td>Hill</td>\n",
       "      <td>608</td>\n",
       "      <td>Spain</td>\n",
       "      <td>Female</td>\n",
       "      <td>41</td>\n",
       "      <td>1</td>\n",
       "      <td>83807.86</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>112542.58</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>15619304</td>\n",
       "      <td>Onio</td>\n",
       "      <td>502</td>\n",
       "      <td>France</td>\n",
       "      <td>Female</td>\n",
       "      <td>42</td>\n",
       "      <td>8</td>\n",
       "      <td>159660.80</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>113931.57</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>15701354</td>\n",
       "      <td>Boni</td>\n",
       "      <td>699</td>\n",
       "      <td>France</td>\n",
       "      <td>Female</td>\n",
       "      <td>39</td>\n",
       "      <td>1</td>\n",
       "      <td>0.00</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>93826.63</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>15737888</td>\n",
       "      <td>Mitchell</td>\n",
       "      <td>850</td>\n",
       "      <td>Spain</td>\n",
       "      <td>Female</td>\n",
       "      <td>43</td>\n",
       "      <td>2</td>\n",
       "      <td>125510.82</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>79084.10</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   RowNumber  CustomerId   Surname  CreditScore Geography  Gender  Age  \\\n",
       "0          1    15634602  Hargrave          619    France  Female   42   \n",
       "1          2    15647311      Hill          608     Spain  Female   41   \n",
       "2          3    15619304      Onio          502    France  Female   42   \n",
       "3          4    15701354      Boni          699    France  Female   39   \n",
       "4          5    15737888  Mitchell          850     Spain  Female   43   \n",
       "\n",
       "   Tenure    Balance  NumOfProducts  HasCrCard  IsActiveMember  \\\n",
       "0       2       0.00              1          1               1   \n",
       "1       1   83807.86              1          0               1   \n",
       "2       8  159660.80              3          1               0   \n",
       "3       1       0.00              2          0               0   \n",
       "4       2  125510.82              1          1               1   \n",
       "\n",
       "   EstimatedSalary  Exited  \n",
       "0        101348.88       1  \n",
       "1        112542.58       0  \n",
       "2        113931.57       1  \n",
       "3         93826.63       0  \n",
       "4         79084.10       0  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Encoding categorical data\n",
    "from sklearn.preprocessing import LabelEncoder, OneHotEncoder\n",
    "labelencoder_X_1 = LabelEncoder()\n",
    "X[:, 1] = labelencoder_X_1.fit_transform(X[:, 1])\n",
    "labelencoder_X_2 = LabelEncoder()\n",
    "X[:, 2] = labelencoder_X_2.fit_transform(X[:, 2])\n",
    "onehotencoder = OneHotEncoder(categorical_features = [1])\n",
    "X = onehotencoder.fit_transform(X).toarray()\n",
    "#to avoid dummy variable trap\n",
    "X = X[:, 1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Splitting the dataset into the Training set and Test set\n",
    "from sklearn.model_selection import train_test_split\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Feature Scaling\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "sc = StandardScaler()\n",
    "X_train = sc.fit_transform(X_train)\n",
    "X_test = sc.transform(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train=X_train.T\n",
    "X_test=X_test.T\n",
    "y_train=y_train.reshape(y_train.shape[0],1)\n",
    "y_test=y_test.reshape(y_test.shape[0],1)\n",
    "y_train=y_train.T\n",
    "y_test=y_test.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "shape_X=X_train.shape\n",
    "shape_Y=y_train.shape\n",
    "m=X_train.shape[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The shape of X is: (11, 8000)\n",
      "The shape of Y is: (1, 8000)\n",
      "I have m = 8000 training examples!\n"
     ]
    }
   ],
   "source": [
    "print ('The shape of X is: ' + str(shape_X))\n",
    "print ('The shape of Y is: ' + str(shape_Y))\n",
    "print ('I have m = %d training examples!' % (m))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def layer_sizes(X,Y):\n",
    "    n_x=X.shape[0]\n",
    "    n_h=10\n",
    "    n_y=Y.shape[0]\n",
    "    return (n_x,n_h,n_y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "(n_x,n_h,n_y)=layer_sizes(X_train,y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The size of the input layer is: n_x = 11\n",
      "The size of the hidden layer is: n_h = 10\n",
      "The size of the output layer is: n_y = 1\n"
     ]
    }
   ],
   "source": [
    "print(\"The size of the input layer is: n_x = \" + str(n_x))\n",
    "print(\"The size of the hidden layer is: n_h = \" + str(n_h))\n",
    "print(\"The size of the output layer is: n_y = \" + str(n_y))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def initialize_parameters(n_x,n_h,n_y):\n",
    "    W1=np.random.randn(n_h,n_x)*0.01\n",
    "    b1=np.zeros((n_h,1))\n",
    "    W2=np.random.randn(n_y,n_h)*0.01\n",
    "    b2=np.zeros((n_y,1))\n",
    "    parameters={\n",
    "            \"W1\":W1,\n",
    "            \"b1\":b1,\n",
    "            \"W2\":W2,\n",
    "            \"b2\":b2}\n",
    "    return parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "W1 = [[-0.01087226  0.00133866 -0.0046199  -0.00880013  0.00554151 -0.00848744\n",
      "  -0.00674436 -0.00749836  0.00412577 -0.00665255  0.00333818]\n",
      " [ 0.00734115  0.00680086 -0.00594362  0.00818413 -0.02312955  0.00416452\n",
      "  -0.00407299  0.00810755 -0.01488571  0.01309719  0.00847865]\n",
      " [ 0.00637767 -0.01013773  0.00567197  0.02009643 -0.0008565  -0.00567022\n",
      "  -0.00018738 -0.00196056  0.0131834   0.01114026 -0.02368298]\n",
      " [-0.00116807  0.01105694 -0.00320739 -0.00598348 -0.01527274  0.02210257\n",
      "  -0.00620276  0.0056699   0.02026517  0.00800886  0.01078677]\n",
      " [ 0.0238804  -0.00083884 -0.00781985  0.02533432 -0.01859611  0.00259433\n",
      "   0.00336505  0.00882712 -0.01710771  0.00020153  0.00568872]\n",
      " [-0.00238332 -0.0046547   0.00050486  0.00482879 -0.00559597 -0.01534744\n",
      "   0.00435732 -0.00986108 -0.00719261 -0.00999617  0.01811962]\n",
      " [ 0.00873483  0.00010439 -0.01126518  0.00172401  0.00704141  0.00849009\n",
      "  -0.0011366   0.00573551  0.0073765   0.02136174 -0.01707167]\n",
      " [ 0.00144764 -0.01136527 -0.01900881  0.00050696  0.00351707 -0.0063843\n",
      "  -0.00427041 -0.00353275 -0.01029429  0.00928287 -0.0093302 ]\n",
      " [-0.01890763 -0.00750409 -0.01042207  0.004032    0.01636672  0.01661665\n",
      "   0.01106668 -0.00343026  0.00413833  0.0008008   0.00067191]\n",
      " [ 0.01172986 -0.00715289 -0.004891    0.01076468  0.01180099 -0.00804281\n",
      "   0.01056505 -0.01825399 -0.00749377 -0.00125649 -0.01102677]]\n",
      "b1 = [[0.]\n",
      " [0.]\n",
      " [0.]\n",
      " [0.]\n",
      " [0.]\n",
      " [0.]\n",
      " [0.]\n",
      " [0.]\n",
      " [0.]\n",
      " [0.]]\n",
      "W2 = [[ 0.01498364 -0.01280093  0.03080759 -0.00998165  0.01437381  0.00404962\n",
      "   0.00271538 -0.02198912 -0.00861461 -0.02004754]]\n",
      "b2 = [[0.]]\n"
     ]
    }
   ],
   "source": [
    "parameters=initialize_parameters(n_x,n_h,n_y)\n",
    "print(\"W1 = \" + str(parameters[\"W1\"]))\n",
    "print(\"b1 = \" + str(parameters[\"b1\"]))\n",
    "print(\"W2 = \" + str(parameters[\"W2\"]))\n",
    "print(\"b2 = \" + str(parameters[\"b2\"]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "def forward_propagation(X,parameters):\n",
    "    W1=parameters[\"W1\"]\n",
    "    b1=parameters[\"b1\"]\n",
    "    W2=parameters[\"W2\"]\n",
    "    b2=parameters[\"b2\"]\n",
    "    \n",
    "    Z1=np.dot(W1,X)+b1\n",
    "    A1=np.tanh(Z1)\n",
    "    Z2=np.dot(W2,A1)+b2\n",
    "    A2=1/(1+np.exp(-Z2))\n",
    "    \n",
    "    cache={\n",
    "            \"Z1\":Z1,\n",
    "            \"A1\":A1,\n",
    "            \"Z2\":Z2,\n",
    "            \"A2\":A2}\n",
    "    return A2,cache"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "A2,cache=forward_propagation(X_train,parameters)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "def compute_cost(A2,Y,parameters):\n",
    "    m=Y.shape[1]\n",
    "    logprobs=np.multiply(np.log(A2),Y)+np.multiply(np.log(1-A2),(1-Y))\n",
    "    cost=-(1/m)*np.sum(logprobs)\n",
    "    \n",
    "    return cost"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cost = 0.6931907346250585\n"
     ]
    }
   ],
   "source": [
    "print(\"cost = \" + str(compute_cost(A2, y_train, parameters)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "def backward_propagation(parameters,cache,X,Y):\n",
    "    m=X.shape[1]\n",
    "    W1=parameters[\"W1\"]\n",
    "    b1=parameters[\"b1\"]\n",
    "    W2=parameters[\"W2\"]\n",
    "    b2=parameters[\"b2\"]\n",
    "    \n",
    "    A1=cache[\"A1\"]\n",
    "    A2=cache[\"A2\"]\n",
    "    \n",
    "    dZ2=A2-Y\n",
    "    dW2=(1/m)*np.dot(dZ2,A1.T)\n",
    "    db2=(1/m)*np.sum(dZ2,axis=1,keepdims=True)\n",
    "    dZ1 = np.multiply((np.dot(W2.T,dZ2)),(1-np.power(A1,2)))\n",
    "    dW1 = 1/(m)*np.dot(dZ1,X.T)\n",
    "    db1 = 1/(m)*np.sum(dZ1,axis=1,keepdims=True)\n",
    "    \n",
    "    grads = {\"dW1\": dW1,\n",
    "             \"db1\": db1,\n",
    "             \"dW2\": dW2,\n",
    "             \"db2\": db2}\n",
    "    return grads"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dW1 = [[-1.05826416e-03  2.60743237e-04  1.75406527e-04  6.42455416e-04\n",
      "  -1.71560237e-03  1.56202930e-04 -7.14166166e-04  3.24459508e-04\n",
      "   7.64052394e-05  9.09123706e-04 -6.28841947e-05]\n",
      " [ 9.02435255e-04 -2.21588298e-04 -1.50337949e-04 -5.47987474e-04\n",
      "   1.46485572e-03 -1.32794348e-04  6.08734898e-04 -2.76265951e-04\n",
      "  -6.73892821e-05 -7.74326302e-04  5.46104520e-05]\n",
      " [-2.17218162e-03  5.34809843e-04  3.59863375e-04  1.31963895e-03\n",
      "  -3.52314500e-03  3.21380318e-04 -1.46551444e-03  6.66328713e-04\n",
      "   1.57301094e-04  1.86720945e-03 -1.27503685e-04]\n",
      " [ 7.02495256e-04 -1.71939623e-04 -1.16774921e-04 -4.27863378e-04\n",
      "   1.14160031e-03 -1.02867795e-04  4.73977036e-04 -2.15537720e-04\n",
      "  -5.10767528e-05 -6.04406633e-04  4.25565561e-05]\n",
      " [-1.01703487e-03  2.50521310e-04  1.68281833e-04  6.15898153e-04\n",
      "  -1.64533996e-03  1.49425102e-04 -6.85378650e-04  3.10495552e-04\n",
      "   7.47912996e-05  8.69730968e-04 -6.05541199e-05]\n",
      " [-2.85414376e-04  7.02835800e-05  4.74091102e-05  1.73589747e-04\n",
      "  -4.63653240e-04  4.22038786e-05 -1.92712365e-04  8.78283853e-05\n",
      "   2.06414676e-05  2.45678280e-04 -1.69393566e-05]\n",
      " [-1.91448716e-04  4.72074954e-05  3.18133597e-05  1.16339009e-04\n",
      "  -3.10899044e-04  2.82885417e-05 -1.29257808e-04  5.88061245e-05\n",
      "   1.38363193e-05  1.64814194e-04 -1.13871209e-05]\n",
      " [ 1.54990827e-03 -3.81112885e-04 -2.57227626e-04 -9.42559767e-04\n",
      "   2.51705271e-03 -2.29056040e-04  1.04655180e-03 -4.76169154e-04\n",
      "  -1.12473551e-04 -1.33430408e-03  9.24219545e-05]\n",
      " [ 6.07956799e-04 -1.49809953e-04 -1.00523427e-04 -3.69276081e-04\n",
      "   9.86785745e-04 -9.00573530e-05  4.10407564e-04 -1.86703851e-04\n",
      "  -4.40042774e-05 -5.22145652e-04  3.59933551e-05]\n",
      " [ 1.41135840e-03 -3.45995804e-04 -2.33774491e-04 -8.60447862e-04\n",
      "   2.29269182e-03 -2.07637376e-04  9.50739769e-04 -4.32867211e-04\n",
      "  -1.01607827e-04 -1.21489561e-03  8.57690234e-05]]\n",
      "db1 = [[ 0.00443334]\n",
      " [-0.00378533]\n",
      " [ 0.00910621]\n",
      " [-0.00295018]\n",
      " [ 0.00424681]\n",
      " [ 0.00119758]\n",
      " [ 0.00080281]\n",
      " [-0.00650347]\n",
      " [-0.00254744]\n",
      " [-0.0059271 ]]\n",
      "dW2 = [[-0.00060211  0.00362468  0.00114969  0.00291989  0.00138177 -0.00035961\n",
      "   0.00019417 -0.00029276 -0.00098139 -0.00294677]]\n",
      "db2 = [[0.296]]\n"
     ]
    }
   ],
   "source": [
    "grads = backward_propagation(parameters, cache, X_train, y_train)\n",
    "print (\"dW1 = \"+ str(grads[\"dW1\"]))\n",
    "print (\"db1 = \"+ str(grads[\"db1\"]))\n",
    "print (\"dW2 = \"+ str(grads[\"dW2\"]))\n",
    "print (\"db2 = \"+ str(grads[\"db2\"]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "def update_parameters(parameters, grads, learning_rate = 1.2):\n",
    "    W1 = parameters[\"W1\"]\n",
    "    b1 = parameters[\"b1\"]\n",
    "    W2 = parameters[\"W2\"]\n",
    "    b2 = parameters[\"b2\"]\n",
    "    \n",
    "    dW1 = grads[\"dW1\"]\n",
    "    db1 = grads[\"db1\"]\n",
    "    dW2 = grads[\"dW2\"]\n",
    "    db2 = grads[\"db2\"]\n",
    "    \n",
    "    W1 = W1-((learning_rate)*dW1)\n",
    "    b1 = b1-((learning_rate)*db1)\n",
    "    W2 = W2-((learning_rate)*dW2)\n",
    "    b2 = b2-((learning_rate)*db2)\n",
    "    \n",
    "    parameters = {\"W1\": W1,\n",
    "                  \"b1\": b1,\n",
    "                  \"W2\": W2,\n",
    "                  \"b2\": b2}\n",
    "    return parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "parameters = update_parameters(parameters, grads)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "W1 = [[-9.60234653e-03  1.02576656e-03 -4.83038966e-03 -9.57108108e-03\n",
      "   7.60022865e-03 -8.67487973e-03 -5.88736369e-03 -7.88771127e-03\n",
      "   4.03408261e-03 -7.74349368e-03  3.41363938e-03]\n",
      " [ 6.25822745e-03  7.06676264e-03 -5.76321374e-03  8.84171429e-03\n",
      "  -2.48873819e-02  4.32387158e-03 -4.80347058e-03  8.43906666e-03\n",
      "  -1.48048400e-02  1.40263838e-02  8.41311449e-03]\n",
      " [ 8.98428838e-03 -1.07795020e-02  5.24013670e-03  1.85128623e-02\n",
      "   3.37127614e-03 -6.05587843e-03  1.57123577e-03 -2.76014947e-03\n",
      "   1.29946382e-02  8.89960372e-03 -2.35299801e-02]\n",
      " [-2.01106142e-03  1.12632708e-02 -3.06725615e-03 -5.47004859e-03\n",
      "  -1.66426588e-02  2.22260073e-02 -6.77153173e-03  5.92854394e-03\n",
      "   2.03264612e-02  8.73415240e-03  1.07357006e-02]\n",
      " [ 2.51008414e-02 -1.13946859e-03 -8.02178745e-03  2.45952395e-02\n",
      "  -1.66217000e-02  2.41501855e-03  4.18750284e-03  8.45452884e-03\n",
      "  -1.71974577e-02 -8.42149090e-04  5.76138208e-03]\n",
      " [-2.04082578e-03 -4.73904485e-03  4.47965263e-04  4.62048006e-03\n",
      "  -5.03958502e-03 -1.53980821e-02  4.58857806e-03 -9.96647322e-03\n",
      "  -7.21737628e-03 -1.02909802e-02  1.81399468e-02]\n",
      " [ 8.96457023e-03  4.77414418e-05 -1.13033531e-02  1.58439918e-03\n",
      "   7.41448875e-03  8.45614850e-03 -9.81486347e-04  5.66494433e-03\n",
      "   7.35989266e-03  2.11639648e-02 -1.70580060e-02]\n",
      " [-4.12252366e-04 -1.09079385e-02 -1.87001387e-02  1.63803062e-03\n",
      "   4.96603396e-04 -6.10943375e-03 -5.52627534e-03 -2.96134523e-03\n",
      "  -1.01593225e-02  1.08840326e-02 -9.44110738e-03]\n",
      " [-1.96371736e-02 -7.32431719e-03 -1.03014397e-02  4.47513605e-03\n",
      "   1.51825814e-02  1.67247169e-02  1.05741890e-02 -3.20621046e-03\n",
      "   4.19113408e-03  1.42737010e-03  6.28719371e-04]\n",
      " [ 1.00362348e-02 -6.73769476e-03 -4.61046912e-03  1.17972127e-02\n",
      "   9.04975697e-03 -7.79364681e-03  9.42415765e-03 -1.77345529e-02\n",
      "  -7.37183704e-03  2.01387704e-04 -1.11296896e-02]]\n",
      "b1 = [[-0.00532001]\n",
      " [ 0.0045424 ]\n",
      " [-0.01092746]\n",
      " [ 0.00354021]\n",
      " [-0.00509617]\n",
      " [-0.00143709]\n",
      " [-0.00096338]\n",
      " [ 0.00780417]\n",
      " [ 0.00305693]\n",
      " [ 0.00711252]]\n",
      "W2 = [[ 0.01570617 -0.01715055  0.02942796 -0.01348553  0.01271569  0.00448115\n",
      "   0.00248238 -0.02163781 -0.00743695 -0.01651142]]\n",
      "b2 = [[-0.3552]]\n"
     ]
    }
   ],
   "source": [
    "print(\"W1 = \" + str(parameters[\"W1\"]))\n",
    "print(\"b1 = \" + str(parameters[\"b1\"]))\n",
    "print(\"W2 = \" + str(parameters[\"W2\"]))\n",
    "print(\"b2 = \" + str(parameters[\"b2\"]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "def nn_model(X, Y, n_h, num_iterations = 10000, print_cost=False):\n",
    "    np.random.seed(3)\n",
    "    n_x = layer_sizes(X, Y)[0]\n",
    "    n_y = layer_sizes(X, Y)[2]\n",
    "    \n",
    "    parameters = initialize_parameters(n_x, n_h, n_y)\n",
    "    W1 = parameters[\"W1\"]\n",
    "    b1 = parameters[\"b1\"]\n",
    "    W2 = parameters[\"W2\"]\n",
    "    b2 = parameters[\"b2\"]\n",
    "    \n",
    "    for i in range(0, num_iterations):\n",
    "         A2, cache = forward_propagation(X,parameters)\n",
    "         cost = compute_cost(A2,Y,parameters)\n",
    "         grads = backward_propagation(parameters,cache,X,Y)\n",
    "         parameters = update_parameters(parameters,grads,2)\n",
    "         \n",
    "         if print_cost and i % 1000 == 0:\n",
    "            print (\"Cost after iteration %i: %f\" %(i, cost))\n",
    "    return parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cost after iteration 0: 0.693124\n",
      "Cost after iteration 1000: 0.320580\n",
      "Cost after iteration 2000: 0.318329\n",
      "Cost after iteration 3000: 0.317805\n",
      "Cost after iteration 4000: 0.317461\n",
      "Cost after iteration 5000: 0.317251\n",
      "Cost after iteration 6000: 0.317111\n",
      "Cost after iteration 7000: 0.316960\n",
      "Cost after iteration 8000: 0.316730\n",
      "Cost after iteration 9000: 0.316176\n"
     ]
    }
   ],
   "source": [
    "parameters = nn_model(X_train, y_train, 10, num_iterations=10000, print_cost=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "def predict(parameters, X):\n",
    "    A2, cache = forward_propagation(X,parameters)\n",
    "    predictions = (A2>0.5)*1\n",
    "    return predictions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "predictions mean = 0.148\n"
     ]
    }
   ],
   "source": [
    "y_pred = predict(parameters, X_test)\n",
    "print(\"predictions mean = \" + str(np.mean(y_pred)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Making the Confusion Matrix\n",
    "from sklearn.metrics import confusion_matrix\n",
    "cm = confusion_matrix(y_test.T, y_pred.T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1511,   84],\n",
       "       [ 193,  212]], dtype=int64)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "accuracy=100*(cm[0][0]+cm[1][1])/X_test.shape[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "86.15%\n"
     ]
    }
   ],
   "source": [
    "print(str(accuracy)+ \"%\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
